#pragma once
#include<iostream>
#include<unistd.h>
#include<cstdlib>
#include<string>
#include<memory>
#include"Logger.hpp"
#include"InetAddr.hpp"

enum
{
    OK,
    CREATE_ERR,
    BIND_ERR,
    LISTEN_ERR,
};

const int gbacklog =8;
const int gsockfd = -1;

class Socket
{
public:
    virtual ~Socket() {}
    virtual void CreateSocketOrDie() = 0; 
    virtual void BindSocketOrDie(int port) = 0; 
    virtual void ListenSocketOrDie(int backlog) = 0;
    virtual int Accept(InetAddr* clinetaddr) = 0;  
    // virtual std::shared_ptr<Socket> Accept(InetAddr* clinetaddr) = 0;  
    virtual int SockFd() = 0;
    virtual void Close() = 0;
    virtual ssize_t Recv(std::string*out) = 0;
    virtual ssize_t Send(const std::string &in) = 0;
    virtual bool Connect(InetAddr &peer) = 0;
public:
    void BuildlistenSocketMethod(int port)
    {
        CreateSocketOrDie();
        BindSocketOrDie(port);
        ListenSocketOrDie(gbacklog);
    }
    void BuildClientSocketMethod()
    {
        CreateSocketOrDie();
    }
};

class TcpSocket:public Socket
{
public:
    TcpSocket(int sockfd=gsockfd)
        :_sockfd(sockfd)
    {}
    void CreateSocketOrDie() override
    {
        _sockfd = socket(AF_INET,SOCK_STREAM,0);
        if(_sockfd < 0)
        {
            LOG(Loglevel::FATAL)<<"creat sockfd error!";
            exit(CREATE_ERR);
        }
        LOG(Loglevel::INFO)<<"create socket success!";
    }
    void BindSocketOrDie(int port) override
    {
        InetAddr local(port);
        if(bind(_sockfd,local.Addr(),local.Length())!=0)
        {
            LOG(Loglevel::FATAL)<<"bind socket error!";
            exit(BIND_ERR);
        }
        LOG(Loglevel::INFO) << "bind socket success!";
    }
    void ListenSocketOrDie(int backlog) override
    {
        if(listen(_sockfd, backlog) != 0)
        {
            LOG(Loglevel::FATAL) << "listen socket error!";
            exit(LISTEN_ERR);
        }
        LOG(Loglevel::INFO) << "listen socket success!";
    }
    int Accept(InetAddr* clinetaddr)override
    // std::shared_ptr<Socket> Accept(InetAddr* clinetaddr)override
    {
        struct sockaddr_in peer;
        socklen_t len=sizeof(peer);

        int fd=accept(_sockfd,(struct sockaddr*)&peer, &len);
        if(fd < 0)
        {
            LOG(Loglevel::WARNING)<<"accept socket error!";
            return -1;
        }
        LOG(Loglevel::INFO)<<"accept socket success!";
        clinetaddr->Init(peer);
        return fd;
    }
    int SockFd() override
    {
        return _sockfd;
    }
    void Close() override
    {
        if(_sockfd >= 0)
            close(_sockfd);
    }
    ssize_t Recv(std::string *out)
    {
        char buffer[1024];
        ssize_t n=recv(_sockfd,buffer,sizeof(buffer)-1,0);
        if(n>0)
        {
            buffer[n]=0;
            *out+=buffer;
        }
        return n;
    }
    ssize_t Send(const std::string &in)
    {
        return send(_sockfd,in.c_str(),in.size(),0);
    }
    bool Connect(InetAddr&peer)override
    {
        int n=connect(_sockfd,peer.Addr(),peer.Length());
        if(n>=0)
            return true;
        else 
            return false;
    }
    ~TcpSocket()
    {}
private:
    int _sockfd;
};